<?php
/**
 *
 * 版权所有：忠赢网站管理系统<www.91zoy.com>

 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台控制器演示。
 *
 **/
namespace Home\Controller;

use Vendor\Page2;
class IndexController extends ComController
{
    public function index()
    {
        // 轮播图
        $flash = M('flash as a');
        $orderby = "a.o ASC";
        $field = "a.title,a.url,a.pic,a.pid,a.o";
        $pic_list = $flash->field($field)
            ->order($orderby)
            ->select();
//        pp($pic_list);exit();
        $banner_list = $sc_list = $fc_list = array();
        if(!empty($pic_list)){
            foreach ($pic_list as $key=>$val){
                switch ($val['pid']){
                    case 1:
                        $banner_list[] = $val;
                        break;
                    case 3:
                        $sc_list[] = $val;
                        break;
                    case 4:
                        $fc_list[] = $val;
                        break;
                }
            }
        }
        $this->assign('banner_list',$banner_list);
        // 生产环境
        $this->assign('sc_list',$sc_list);
        // 企业风采
        $this->assign('fc_list',$fc_list);
        // 资质
        $zizhi_list = $flash->field($field)
            ->where("a.pid = 2")
            ->limit(3)
            ->order($orderby)
            ->select();
        if(!empty($zizhi_list)){
            foreach ($zizhi_list as $key => $value){
                if(empty($value['url']) || ($value['url'] == "#")){
                    $zizhi_list[$key]['url'] = "javascript:;";
                }
            }
        }
        $this->assign('zizhi_list',$zizhi_list);
        // 新闻类
        $article = M('article as a');
        $orderby = "a.t DESC";
        $where = "isPushed = 0 AND ";
        $prefix = C('DB_PREFIX');
        $field = "a.aid,a.sid,a.title,a.description,a.thumbnail,a.t,{$prefix}category.name,{$prefix}category.dir";
        // 解决方案
        $fangan = $article->field($field)
            ->where($where." sid = 1")
            ->order($orderby)
            ->limit(6)
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->select();
        $this->assign('fangan',$fangan);
        // 客户见证
        $jianzheng = $article->field($field)
            ->where($where." sid = 2")
            ->order($orderby)
            ->limit(4)
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->select();
        $this->assign('jianzheng',$jianzheng);
        // 新闻
        $bosen = $article->field($field)
            ->where($where." sid = 3")
            ->order($orderby)
            ->limit(3)
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->select();
        $this->assign('bosen',$bosen);
        // 百科
        $baike = $article->field($field)
            ->where($where." sid = 4")
            ->order($orderby)
            ->limit(3)
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->select();
        $this->assign('baike',$baike);
        // 问答
        $wenda = $article->field($field)
            ->where($where." sid = 5")
            ->order($orderby)
            ->limit(3)
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->select();
        $this->assign('wenda',$wenda);
        // 产品
        $product_m = M('product as b');
        $orderby = "b.product_id ASC";
        $prefix = C('DB_PREFIX');
        $field = "b.*,{$prefix}pcategory.title";
        $product = $product_m->field($field)
            ->order($orderby)
            ->limit(4)
            ->join("{$prefix}pcategory ON {$prefix}pcategory.id = b.cid")
            ->select();
        if(!empty($product)){
            foreach ($product as $key=>$val){
                $cover = M('photos')->where(array('item_id'=>$val['product_id']))->find();
                $product[$key]['cover'] = $cover["image_url"];
            }
        }
        $this->assign('product',$product);
        $product2 = $product_m->field($field)
            ->order($orderby)
            ->join("{$prefix}pcategory ON {$prefix}pcategory.id = b.cid")
            ->find();
        if(!empty($product2)){
            $pro_img = M('photos')->where(array('item_id'=>$product2['product_id']))->find();
            $product2['cover'] =  $pro_img["image_url"];
            $product2['desc'] = mb_substr(preg_replace("/<\/?.*?>/", "", $product2['content']), 0 ,40)."……";
        }
//        pp(strip_tags($product2["content"]));exit();
        $this->assign('product2',$product2);
        // 团队介绍单页
        $pages = M('pages as a');
        $field = "a.title,a.description";
        $about = $pages->field($field)->find(1);
        $this->assign('about',$about);
		$this->display();
	}

    /**
     * 新闻
     */
    public function news(){

        $p = intval(I('p')) > 0 ? I('p') : 1;
        $sid = intval(I('sid')) > 0 ? I('sid') : 0;
        $article = M('article as a');
        if(!empty($sid)){
            $where = "sid = $sid";
            $category = M("category")->find($sid);
            $this->assign('categoryName',$category['name']);
            $this->assign('category',$category);
        }else{
            $where = "1 = 1";
        }
        // 贷款资讯中间
        $orderby = "a.t DESC";
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $field = "a.aid,a.title,a.description,a.thumbnail,a.t,{$prefix}category.name,{$prefix}category.dir";
        $count = $article->where($where)->count();
        $list = $article->field($field)
            ->where($where)
            ->order($orderby)
            ->join("{$prefix}category ON {$prefix}category.id = a.sid")
            ->limit($offset . ',' . $pagesize)
            ->select();
        $page = new \Think\Page2($count, $pagesize);
        $page = $page->show();
        $this->assign('list',$list);
        $this->assign('page',$page);
        $category_list = M("category")->select();
        $this->assign('category_list',$category_list);
        $this->assign('category_page',1);
        $this->display();
    }

    /**
     * 新闻详情页
     */
    public function page(){

        $aid = intval(I('aid')) > 0 ? I('aid') : false;
        if(!$aid){
            $this->display("404");
        }
        $article = M('article')->where('aid='.$aid)->find();
        $this->assign('article',$article);
        $sid = $article['sid'];
        $category = M("category")->find($sid);
        $this->assign('categoryName',$category['name']);
        $this->assign('category',$category);
        $upWhere['aid'] = ['gt',$aid];
        $upWhere['sid'] = $sid;
        $up   = $this->newsDetail($upWhere,'aid desc');
        $this->assign('up',$up);
        $nextWhere['aid'] = ['lt',$aid];
        $nextWhere['sid'] = $sid;
        $next = $this->newsDetail($nextWhere,'aid desc');
        $this->assign('next',$next);
        $this->assign('nav',$aid);
        // 浏览量
        $visit_session = session("visit_ip_$aid");
        if(!isset($visit_session) && empty($visit_session)){
            M('article')->where(array("aid"=>$aid))->setInc('browse',1);
            session("visit_ip_$aid",1);
        }
        $category_list = M("category")->select();
        $this->assign('category_list',$category_list);
        $this->assign('article_page',1);
        $this->display();
    }
    public function newsDetail($where,$order)
    {
        $data = M('article')->where($where)->order($order)->find();
        return $data;
    }

    /**
     * 关于我们
     */
    public function about(){
        // 团队介绍单页
        $pages = M('pages as a');
        $field = "a.title,a.content";
        $introduct = $pages->field($field)->find(2);
        $this->assign('introduct',$introduct);
        $culture = $pages->field($field)->find(1);
        $this->assign('culture',$culture);
        $this->assign('nav',1);
        $this->display();
    }
    public function fengcai(){
        // 轮播图
        $flash = M('flash as a');
        $orderby = "a.o ASC";
        $field = "a.title,a.url,a.pic,a.pid,a.o";
        $fc_list = $flash->field($field)
            ->where(array("pid"=>4))
            ->order($orderby)
            ->select();
        // 企业风采
        $this->assign('fc_list',$fc_list);
        $this->assign('nav',2);
        $this->display();
    }

    public function huanjing(){
        // 轮播图
        $flash = M('flash as a');
        $orderby = "a.o ASC";
        $field = "a.title,a.url,a.pic,a.pid,a.o";
        $hj_list = $flash->field($field)
            ->where(array("pid"=>3))
            ->order($orderby)
            ->select();
        // 企业风采
        $this->assign('nav',3);
        $this->assign('hj_list',$hj_list);
        $this->display();
    }

    public function application(){
        if($_POST){
            $data['name'] = isset($_POST['username']) ? $_POST['username'] : false;
            $data['mobile'] = isset($_POST['tel']) ? $_POST['tel'] : false;
            $data['content'] = isset($_POST['content']) ? $_POST['content'] : false;
            $data['mail'] = isset($_POST['mail']) ? $_POST['mail'] : "";
            $data['wechat'] = isset($_POST['wechat']) ? $_POST['wechat'] : "";
            $data['ctime']= date('Y-m-d H:i:s');
            if (!$data['mobile']) {
                $this->ajaxReturn(["code"=>400,"msg"=>"请填写号码！"]);
            }
            if (M('message')->add($data)) {
                $this->ajaxReturn(["code"=>200,"msg"=>"提交成功！"]);
            } else {
                $this->ajaxReturn(["code"=>400,"msg"=>"提交失败！"]);
            }
        }else{
            $this->display();
        }
    }

    public function honor(){
        // 资质
        $flash = M('flash as a');
        $orderby = "a.o ASC";
        $field = "a.title,a.url,a.pic";
        $zizhi_list = $flash->field($field)
            ->where("a.pid = 2")
            ->order($orderby)
            ->select();
        if(!empty($zizhi_list)){
            foreach ($zizhi_list as $key => $value){
                if(empty($value['url']) || ($value['url'] == "#")){
                    $zizhi_list[$key]['url'] = "javascript:;";
                }
            }
        }
        $this->assign('zizhi_list',$zizhi_list);
        $this->display();
    }

    public function contact(){

        $this->display();
    }

    public function product(){
        $p = intval(I('p')) > 0 ? I('p') : 1;
        $cid = intval(I('cid')) > 0 ? I('cid') : 0;
        $article = M('product as a');
        if(!empty($cid)){
            $where = "cid = $cid";
            $pcategory = M("pcategory")->find($cid);
            $this->assign('categoryName',$pcategory['name']);
            $this->assign('pcategory',$pcategory);
        }else{
            $where = "1 = 1";
        }
        // 贷款资讯中间
        $orderby = "a.ctime DESC";
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');
        $field = "a.*,{$prefix}pcategory.title";
        $count = $article->where($where)->count();
        $list = $article->field($field)
            ->where($where)
            ->order($orderby)
            ->join("{$prefix}pcategory ON {$prefix}pcategory.id = a.cid")
            ->limit($offset . ',' . $pagesize)
            ->select();
        if(!empty($list)){
            foreach ($list as $key=>$val){
                $img =  M("photos")->where(array('item_id'=>$val['product_id']))->find();
                $list[$key]['image_url'] = $img['image_url'];
            }
        }
        $page = new \Think\Page2($count, $pagesize);
        $page = $page->show();
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('category_page',1);

        $pcategory_list = M("pcategory")->select();
        $this->assign('pcategory_list',$pcategory_list);
        $this->display();
    }

    public function detail(){
        $id = intval(I('id')) > 0 ? I('id') : false;
        if(!$id){
            $this->display("404");
        }
        $product = M('product')->where('product_id='.$id)->find();
        $this->assign('product',$product);
        $imgs =  M("photos")->where(array('item_id'=>$product['product_id']))->select();
        $this->assign('imgs',$imgs);
        $cid = $product['cid'];
        $category = M("pcategory")->find($cid);
        $this->assign('categoryName',$category['name']);
        $this->assign('category',$category);
        $this->display();
    }

}